package com.simple.json.clazz.basic;

import java.lang.reflect.Field;

import org.json.JSONObject;

import com.simple.json.Util;
import com.simple.json.clazz.ClassInterface;

/**
 * boolean的处理类
 * @author dell
 *
 */
public class BasicBooleanClass implements ClassInterface {

	private BasicBooleanClass() {
	}

	private final static BasicBooleanClass instance = new BasicBooleanClass();

	public static BasicBooleanClass getInstance() {
		return instance;
	}

	@Override
	public Class<?> getProcessClassType() {
		return boolean.class;
	}

	@Override
	public <T> boolean setRightValueToField(T targetObject, Field targetField,
			JSONObject jsonObject) {

		Class<?> fieldClass = targetField.getType();
		if (!fieldClass.getName().equals(getProcessClassType().getName())) {
			return false;
		}
		try {
			boolean value = jsonObject.optBoolean(targetField.getName(), false);
//			targetField.setAccessible(true);
			targetField.setBoolean(targetObject, value);
			Util.logout("set " + targetField.getName() + "'s boolean value = " + value);
			return true;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Object getRightValue(Object value) {
		return BooleanClass.getInstance().getRightValue(value);
	}

}
